package com.example.designpatterns.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * 树枝构件（Composite）
 */
public class FileFolder extends FileComponent {
    //文件夹可以有多个子文件夹或者子文件
    private List<FileComponent> fileComponentList;

    public FileFolder(String name, Integer level, Integer type) {
        this.name = name;
        this.level = level;
        this.type = type;
        this.fileComponentList = new ArrayList<>();
    }

    @Override
    public void add(FileComponent fileComponent) {
        fileComponentList.add(fileComponent);
    }

    @Override
    public void remove(FileComponent fileComponent) {
        fileComponentList.remove(fileComponent);
    }

    @Override
    public FileComponent getChild(int index) {
        return fileComponentList.get(index);
    }

    @Override
    public void print() {
        //打印菜单名称
        for (int i = 0; i < level; i++) {
            System.out.print("\t");
        }
        System.out.println(name);
        //打印子菜单或者子菜单项名称
        for (FileComponent component : fileComponentList) {
            component.print();
        }
    }
}